package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;


    /**
     * 新建套餐，套餐信息包括套餐本身的信息和菜品的信息
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmeal_cache", allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto)
    {
        log.info("套餐信息：{}", setmealDto);

        //保存
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name)
    {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null, Setmeal::getName, name);
        lambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo);

        //目前只要套餐分类id，没有套餐分类name

        //修改一下
        Page<SetmealDto> pageInfoDto = new Page<>();
        BeanUtils.copyProperties(pageInfo, pageInfoDto, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> dtoRecords = records.stream().map((item)->{
            //拷贝一些值
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            //设置类比名字
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if(category != null)
            {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);

            }

            return setmealDto;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(dtoRecords);

        return R.success(pageInfoDto);
    }

    /**
     * 删除套餐，包括单个删除和多个删除
     * 删除包括setmeal表和setmealdish表
     * @return
     */
    @CacheEvict(value = "setmeal_cache", allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids)
    {
        log.info("待删除的ids: {}", ids.toString());
        setmealService.deleteWithDish(ids);

        return R.success("删除成功");
    }
    

    /**
     * 根据条件查询菜品list
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmeal_cache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> getList(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);
    }



}
